package com.sgb.member.ui.activity;

import android.annotation.SuppressLint;
import android.app.Application;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.member.MemberConfig;
import com.sgb.member.entity.ApplyCompanyBean;
import com.sgb.member.entity.CompanyDeptBean;
import com.sgb.member.entity.MemberUpdateLevelBean;
import com.sgb.member.entity.UserMsgEntity;
import com.sgb.member.service.MemberAPI;
import com.sgb.member.weight.UploadFileBean;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.selection.CommonPopEntity;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 会员调级申请
 */
public class MemberTransferModel extends AndroidViewModel {

    public MutableLiveData<Boolean> dialog = new MutableLiveData<>();
    public MutableLiveData<BaseEntity<Object>> mUpdateLevel = new MutableLiveData<>();
    public MutableLiveData<List<CommonPopEntity>> mLevers = new MutableLiveData<>();
    public MutableLiveData<List<ApplyCompanyBean>> mCompanyList = new MutableLiveData<>();
    public MutableLiveData<List<CompanyDeptBean>> mDepList = new MutableLiveData<>();
    public MutableLiveData<String> mApprovalStatus = new MutableLiveData<>(); // STYLE_CONFIRM_APPROVAL
    MemberAPI api;

    public MemberTransferModel(@NonNull Application application) {
        super(application);
        api = MemberConfig.getInstance().getApi();
    }

    // 修改会员等级
    public void memberUpdateLevel(MemberUpdateLevelBean bean) {
        dialog.setValue(true);
        Call<BaseEntity<BaseEntity<Object>>> call;
        if (bean.isPersonMember()) {
            api.memberUpdateLevelForPerson(bean).enqueue(new ICallBack<BaseEntity<Object>>() {
                @Override
                public void onSuccess(BaseEntity<Object> data) {
                    dialog.setValue(false);
                    data.setCode("0");
                    mUpdateLevel.setValue(data);
                }

                @Override
                public void onFail() {
                    dialog.setValue(false);
                }
            });
        } else {
            api.memberUpdateLevel(bean).enqueue(new ICallBack<BaseEntity<BaseEntity<Object>>>() {
                @Override
                public void onSuccess(BaseEntity<BaseEntity<Object>> data) {
                    dialog.setValue(false);
                    mUpdateLevel.setValue(data.getData());
                }

                @Override
                public void onFail() {
                    dialog.setValue(false);
                }
            });
        }
    }

    // 会员等级
    public void getMemberLevelList() {
        dialog.setValue(true);
        api.getMemberLevelList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                List<CommonPopEntity> tempList = data.getData();
                if (tempList != null && tempList.size() > 0) {
                    if (tempList.get(0).getTypeName().equals("全部")) {
                        tempList.remove(0);
                    }
                    mLevers.setValue(tempList);
                }
            }
        });
    }


    // 审批【申请】企业列表
    public void getMyCompanyList() {
        dialog.setValue(true);
        api.getMyCompanyList().enqueue(new ICallBack<BaseEntity<List<ApplyCompanyBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<ApplyCompanyBean>> data) {
                mCompanyList.setValue(data.getData());
            }
        });
    }

    // 审批【申请】部门列表
    public void getDeptList(String companyNo) {
        dialog.setValue(true);
        api.getDeptList(companyNo).enqueue(new ICallBack<BaseEntity<List<CompanyDeptBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CompanyDeptBean>> data) {
                mDepList.setValue(data.getData());
            }
        });
    }

    public MutableLiveData<Boolean> userInfo = new MutableLiveData<>();

    public void getUserInfo() {
        dialog.setValue(true);
        api.getUserInfo().enqueue(new ICallBack<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UserMsgEntity> data) {
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NAME, data.getData().getUserName());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NO, data.getData().getNo());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_NAME, data.getData().getRealName());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.DRIVER_AUTH, data.getData().getIsDriverAuth());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ENTERPRISE_AUTH, data.getData().getEnterpriseAuth());
                if (data.getData().getEnterprise() == null) {
                    userInfo.setValue(false);
                    return;
                }
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, data.getData().getEnterprise().getCompanyName());
                Log.e("公司no", data.getData().getEnterprise().getNo());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, data.getData().getEnterprise().getNo());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_ID, data.getData().getEnterprise().getId());
                userInfo.setValue(true);
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                super.onFailure(call, t);
                userInfo.setValue(false);
            }
        });
    }

    /**
     * 获取审批流程状态
     * <p>
     * approveType 邀请加盟审批类型 inviteJoinCompany
     * 申請加盟审批类型 applyJoinCompany
     * 会员企业调级 memberEvaluate
     * 会员解除申请 memberRelieve
     */
    public void getProcessStatus() {
        dialog.setValue(true);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        api.getProcessStatus(companyNo, "memberEvaluate").enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                // "status": "1" //1 表示有审批 0 代表无审批
                String status = ((Map<String, String>) data.getData()).get("status");
                mApprovalStatus.setValue(status);
            }
        });
    }

    public MutableLiveData<UploadFileBean> mFileData = new MutableLiveData<>();

    public void uploadSingleFile(UploadFileBean item) {
        File file = new File(item.getFilePath());
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"file." + item.getExtension(), requestBody);
        api.uploadFile(photos).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                item.setUrlPath(data.getData());
                mFileData.setValue(item);
            }
        });
    }
}
